{include file="public/header" /}
<div class="ns-screen layui-collapse">
    <form class="layui-colla-content layui-form layui-show">

        <div class="layui-form-item">

            <div class="layui-inline">
                <label class="layui-form-label">所属频道：</label>
                <div class="layui-input-inline">
                    <select name="sex" id="sex">
                        <option value="">全部</option>
                        <option value="1">女</option>
                        <option value="2">男</option>
                    </select>
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">分类名：</label>
                <div class="layui-input-inline">
                    <input type="text" name="keyword" autocomplete="off" class="layui-input" />
                </div>
            </div>

            <input name="type" type="hidden" value="{$type}">

            <div class="layui-inline">
                <button class="layui-btn layui-btn-radius layui-btn-normal" lay-submit lay-filter="search">搜索</button>
            </div>
        </div>

    </form>
    <div class="layui-form-item">
        <button type="button" class="layui-btn layui-btn-radius layui-btn-normal" onclick="add()">添加分类</button>
    </div>
</div>

<table id="list" lay-filter="list"></table>

<!-- 操作列 -->
<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="book">分类小说</a>
</script>

</body>
</html>
{include file="public/footer" /}
<script>
    var table, form, layer, $;
    layui.use(['form', 'table', 'layer'], function(){
        table = layui.table,
            form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //第一个实例
        table.render({
            elem: '#list'
            ,id: 'cateTable'
            ,url: '{:url("index")}'
            ,where: {type:"{$type}"}
            ,page: true //开启分页
            ,cols: [[ //表头
                {field: 'id', title: '序号', sort: true}
                ,{field: 'name', title: '分类名'}
                ,{field: 'sort', title: '排序',sort: true}
                ,{field: 'total_story', title: '作品数量'}
                ,{field: 'tool', title: '操作', toolbar: '#tool',}
            ]],
            parseData: function(res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.count,
                    "data": res.data.list
                };
            },
        });

        table.on('tool(list)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                layer.confirm('确定删除该分类吗', function (index) {
                    $.ajax({
                        url: "{:url('del')}",
                        type: 'post',
                        dataType: 'json',
                        contentType: 'application/json',
                        data: JSON.stringify({id: data.id, type: "{$type}"}),
                        timeout: 2000,
                        success: function (res) {
                            layer.msg(res.msg)
                        },
                        error: function () {
                            layer.msg("删除失败")
                        }
                    })
                    layer.close(index);
                    location.reload();
                });
            } else if (layEvent === 'edit') {
                var index = layer.open({
                    title: '添加菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: "{:url('edit')}?id="+data.id,
                });

                $(window).on("resize", function () {
                    layer.full(index);
                });

            }else if (layEvent === 'book') {
                console.log("暂无");
                // var index = layer.open({
                //     title: '分类作品',
                //     type: 2,
                //     shade: 0.2,
                //     maxmin:true,
                //     shadeClose: true,
                //     area: ['100%', '100%'],
                //     content: '{:url("bookList")}?id=' + data.id,
                // });
                //
                // $(window).on("resize", function () {
                //     layer.full(index);
                // });
            }
        });

        form.on('submit(search)', function(data) {
            table.reload('cateTable', {
                url: '{:url("index")}'
                ,where: data.field
            });
            return false;
        });
    });

    function add() {
        var index = layer.open({
            title: '添加菜单',
            type: 2,
            shade: 0.2,
            maxmin:true,
            shadeClose: true,
            area: ['100%', '100%'],
            content: "{:url('add')}",
        });

        $(window).on("resize", function () {
            layer.full(index);
        });
    }

</script>